package com.bl.weilan.service.biz.tradereport.impl;

import com.bl.weilan.common.ApiResponseCode;
import com.bl.weilan.common.CommonServiceException;
import com.bl.weilan.common.utils.UUIDUtils;
import com.bl.weilan.dao.model.TradeReportEntity;
import com.bl.weilan.dao.model.TradeBEntity;
import com.bl.weilan.dao.model.TradeReportEntity;
import com.bl.weilan.dao.repository.TradeARepository;
import com.bl.weilan.dao.repository.TradeReportRepository;
import com.bl.weilan.service.biz.tradereport.TradeReportService;
import com.bl.weilan.service.vo.DelBody;
import com.bl.weilan.service.vo.DelListBody;
import com.bl.weilan.service.vo.tradereport.TradeReportAddVo;
import com.bl.weilan.service.vo.tradereport.TradeReportListVo;
import com.bl.weilan.service.vo.tradereport.TradeReportModVo;
import com.bl.weilan.service.vo.tradeb.TradeBListVo;
import com.bl.weilan.service.vo.tradereport.TradeReportListVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


@Service
@Log4j2
public class TradeReportServiceImpl implements TradeReportService {

    @Autowired
    TradeReportRepository tradeReportRepository;

    @Override
    public TradeReportListVo getList(String tradeId, String order, Pageable pageable) {
        Page<TradeReportEntity> tradeReportEntities = null;
        TradeReportListVo tradeReportListVo = new TradeReportListVo();

        if(order.equals("desc")) {
            tradeReportEntities = tradeReportRepository.findByTradeIdOrderByTimeDesc(tradeId,pageable);
        } else {
            tradeReportEntities = tradeReportRepository.findByTradeIdOrderByTimeAsc(tradeId,pageable);
        }
        for (TradeReportEntity tradeReportEntity : tradeReportEntities) {
            long si = tradeReportEntity.getSize();
            tradeReportEntity.setSize(si);
        }
        tradeReportListVo.setTotalCount(tradeReportEntities.getTotalElements());
        tradeReportListVo.setList(tradeReportEntities.getContent());
        return tradeReportListVo;
    }

    @Override
    public TradeReportListVo getListManager(String tradeId,Pageable pageable) {
        Page<TradeReportEntity> tradeReportEntities = null;
        TradeReportListVo tradeReportListVo = new TradeReportListVo();
        tradeReportEntities = tradeReportRepository.findByTradeIdOrderByTimeDesc(tradeId,pageable);
        for (TradeReportEntity tradeReportEntity : tradeReportEntities) {
            long si = tradeReportEntity.getSize();
            tradeReportEntity.setSize(si);
        }
        tradeReportListVo.setTotalCount(tradeReportEntities.getTotalElements());
        tradeReportListVo.setList(tradeReportEntities.getContent());
        return tradeReportListVo;
    }


    @Override
    public TradeReportEntity getTradeReportById(String tradereportId) {
        TradeReportEntity tradereportEntity = tradeReportRepository.findOne(tradereportId);
        long si = tradereportEntity.getSize()/1024/1024;
        tradereportEntity.setSize(si);
        return tradereportEntity;
    }

    @Override
    public void addTradeReport(TradeReportAddVo tradereportAddVo) {
        TradeReportEntity tradereportEntity = new TradeReportEntity();
        tradereportEntity.setId(UUIDUtils.getUUID());
        BeanUtils.copyProperties(tradereportAddVo, tradereportEntity);
        tradeReportRepository.save(tradereportEntity);

    }

    @Override
    public void del(DelBody id) {
        tradeReportRepository.delete(id.getId());
    }

    @Override
    public void delList(DelListBody idList) {
        for(String id : idList.getList()) {
            tradeReportRepository.delete(id);
        }
    }

    @Override
    public void modify(TradeReportModVo tradereportModVo){
        TradeReportEntity tradereportEntity = tradeReportRepository.findOne(tradereportModVo.getId());

        if (tradereportEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_INCORRECT.getCode(), "没有对应信息");
        }
        BeanUtils.copyProperties(tradereportModVo, tradereportEntity);
        tradeReportRepository.save(tradereportEntity);
    }
}
